/*******************************************************************************
 * Package: com.hadoop.flow
 * Type:    Flow
 * Date:    2024-05-15 21:48
 *
 * Copyright (c) 2024 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hadoop.flow;

import lombok.Data;
import lombok.ToString;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 功能描述：  流量统计
 * 自定义一个对象类型  实现Writable 结构  方便序列化
 * WritableComparable   TODO 可排序 代码没实现成功
 * Writable 不排序
 * @author Songxianyang
 * @date 2024-05-15 21:48
 */
@Data
@ToString
public class FlowWritable implements WritableComparable<FlowWritable> {
    /**
     * 上传流量
     */
    private Long up;
    /**
     * 下载流量
     */
    private Long down;
    /**
     * 流量求和
     */
    private Long sum;
    /**
     * 手机号
     */
    // private String phoneId;



    public FlowWritable(Long up, Long down) {
        this.up = up;
        this.down = down;
        this.sum = up + down;
    }
    //必须要有，反序列化要调用空参构造器
    public FlowWritable() {
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(up);
        dataOutput.writeLong(down);
        dataOutput.writeLong(sum);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.up = dataInput.readLong();
        this.down = dataInput.readLong();
        this.sum = dataInput.readLong();
    }

    /**
     * 排序
     * @param flowWritable
     * @return int
     */
    @Override
    public int compareTo(FlowWritable flowWritable) {
        //升序
        // return (int) (this.sum-flowWritable.getSum());
        //降序
       return (int) (-this.sum+flowWritable.getSum());
    }
}
